/* eslint-disable no-param-reassign */
import { message } from 'antd';
import { routerRedux } from 'dva/router';
import {
  getCenterList,
  getRuleList,
  saveInvRule,
  getInvRuleCost,
  updateInvCost,
} from '@/services/invrule';

export default {
  state: {
    invRulePanyTermMailDto: [
      {
        id: 0,
        costId: '',
        invCompany: {},
        invMailList: [{ costInvType: 1, invPay: 1 }],
      },
    ],
    // 新增一项时的数据格式
    invRulePanyItem: [
      {
        id: 0,
        costId: '',
        invCompany: {},
        invMailList: [],
      },
    ],
    id: 0,
    // 发票信息集合
    dtoId: '0',
    costInvType: 1,
    centerList: [],
    custCostCenterList: [], // 成本中心下拉列表
    invoiceList: [],
    mailAddrList: [],
    titleAddrData: [],
    titleListData: [],

    titleModalVisible: false,
    addrModalVisible: false,
    titleAddrType: '1',
    costInvTypeId: 1,
  },
  effects: {
    // 获取成本中心列表
    *fetchCenterList({ payload }, { call, put }) {
      const response = yield call(getCenterList, payload);
      if (response.success) {
        yield put({
          type: 'getCenterList',
          payload: response.data,
        });
      } else {
        message.warning(`${response.message || '服务器错误'}`);
      }
    },

    // 获取发票规则列表
    *fetchRuleList({ payload }, { call, put }) {
      const response = yield call(getRuleList, payload);
      yield put({
        type: 'updateModelData',
        payload,
      });
      if (response.success) {
        yield put({
          type: 'getRuleList',
          payload: response.data,
        });
      } else {
        message.warning(`${response.message || '服务器错误'}`);
      }
    },
    *submit({ payload }, { call, put }) {
      const response = yield call(saveInvRule, payload);
      if (response.success) {
        message.success('保存成功!');
        yield put(routerRedux.goBack());
      } else {
        message.warning(`${response.message || '服务器错误'}`);
      }
    },
    *fetchCostDetail({ payload }, { call }) {
      const response = yield call(getInvRuleCost, payload);
      if (response.success) {
        return response.data;
      }
      return message.warning(`${response.message || '服务器错误'}`);
    },
    // 修改成本中心
    *updateInvRuleCost({ payload }, { call, put }) {
      const response = yield call(updateInvCost, payload);
      if (response.success) {
        message.success('修改成功!');
        yield put(routerRedux.goBack());
      } else {
        message.warning(`${response.message || '服务器错误'}`);
      }
    },
  },

  reducers: {
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // handleTitleOrAddr(state, { payload }) {
    //   const { selectedRows } = payload;
    //   const { dtoId, titleAddrType, costInvTypeId } = state;
    //   const arr = [];
    //   // 发票保存
    //   if (titleAddrType === '1') {
    //     state.invRulePanyTermMailDto.forEach(item => {
    //       if (item.id === dtoId) {
    //         item.invCompany = selectedRows[0];
    //       }
    //     });
    //   } else {
    //     state.invRulePanyTermMailDto.forEach(item => {
    //       if (item.id === dtoId) {
    //         item.invMailList.forEach(items => {
    //           if (items.costInvType === costInvTypeId) {
    //             arr.push({ ...items, ...selectedRows[0] });
    //           } else {
    //             arr.push({ ...items });
    //           }
    //         });

    //         item.invMailList = arr;
    //       }
    //     });
    //   }
    //   return {
    //     ...state,
    //   };
    // },

    getCenterList(state, { payload }) {
      const { custCostCenterList, invoiceList, mailAddrList } = payload;
      const invoiceListArr = [];

      invoiceList.forEach(item => {
        const obj = {};
        Object.keys(item).forEach(keys => {
          if (keys === 'vatCoprName') {
            obj.holderName = item[keys];
          } else if (keys === 'invPhone') {
            obj.phone = item[keys];
          } else if (keys === 'invAddr') {
            obj.holderAddrs = item[keys];
          } else {
            obj[keys] = item[keys];
          }
        });
        invoiceListArr.push(obj);
      });
      return {
        ...state,
        custCostCenterList,
        invoiceList: invoiceListArr,
        mailAddrList,
      };
    },
    // 选择交付方式
    invPayChange(state, { payload }) {
      const { dtoId, costInvTypeId, invPay } = payload;
      state.invRulePanyTermMailDto.forEach(item => {
        if (item.id === dtoId) {
          item.invMailList.forEach(items => {
            if (items.costInvType === costInvTypeId) {
              items.invPay = invPay;
            }
          });
        }
      });
      return {
        ...state,
        dtoId,
      };
    },

    setMailArea(state, { payload }) {
      const { dtoId, costInvType, mailArea } = payload;
      state.invRulePanyTermMailDto.forEach(item => {
        if (item.id === dtoId) {
          item.invMailList.forEach(items => {
            if (items.costInvType === costInvType) {
              items.mailArea = mailArea;
            }
          });
        }
      });
      return {
        ...state,
        dtoId,
      };
    },

    getRuleList(state, { payload }) {
      const { invRulePanyTermMailDto } = payload;

      const newArr = invRulePanyTermMailDto.map(item => ({
        costInvType: item.invCompany.invType,
        costInvTypeName: item.invCompany.invTypeName,
        invPay: item.invMail.invPay,
        invPayName: item.invMail.invPayName,
        email: item.invMail.email,
      }));

      state.invRulePanyTermMailDto[0].invMailList = newArr;

      //  const invRulePanyItem = state.invRulePanyTermMailDto.filter(item => item);
      const invRulePanyItem = [
        {
          invMailList: newArr,
          invCompany: {},
          costId: '',
        },
      ];
      // console.log('invRulePanyItem', invRulePanyItem);

      return {
        ...state,
        invRulePanyItem,
      };
    },
    getInvRuleCost(state, { payload }) {
      const { invRulePanyTermMailDto } = payload;
      const areaList = [];
      invRulePanyTermMailDto.forEach(item => {
        item.invMailList.forEach(items => {
          // console.log('items.mailArea', items.mailArea);
          items.areaList.forEach(values => {
            areaList.push(values.areaId);
          });

          items.mailArea = areaList;
        });
      });
      return {
        ...state,
        invRulePanyTermMailDto: payload.invRulePanyTermMailDto,
      };
    },
  },
};
